home *** CD-ROM | disk | FTP | other *** search
/ ftp.cs.arizona.edu / ftp.cs.arizona.edu.tar / ftp.cs.arizona.edu / icon / newsgrp / group02b.txt / 000160_icon-group-sender_Tue Dec 10 07:57:42 2002.msg < prev    next >
Internet Message Format  |  2003-01-02  |  3KB

  1. Return-Path: <icon-group-sender>
  2. Received: (from root@localhost)
  3.     by baskerville.CS.Arizona.EDU (8.11.1/8.11.1) id gBAEvWY04347
  4.     for icon-group-addresses; Tue, 10 Dec 2002 07:57:32 -0700 (MST)
  5. Message-Id: <200212101457.gBAEvWY04347@baskerville.CS.Arizona.EDU>
  6. X-Sender: whm@mail.mse.com
  7. Date: Mon, 09 Dec 2002 20:35:48 -0700
  8. To: icon-group@cs.arizona.edu
  9. From: "William H. Mitchell" <whm@mse.com>
  10. Subject: Hack for partial evaluation in Icon
  11. Errors-To: icon-group-errors@cs.arizona.edu
  12. Status: RO
  13.  
  14. James' mention of ML got me thinking about an ML feature I'd really like to
  15. see in Icon: partial evaluation -- the ability to supply some parameters
  16. for a function now and some later, with the function call taking place when
  17. all parameters have been supplied.
  18.  
  19. It's a hack but here's a way to get that effect in Icon, albeit with an
  20. unfortunate finiteness...
  21.  
  22. Here's a main program:
  23.  
  24. invocable all
  25. link image
  26. procedure main()
  27.     lftx := partial(left, "x")
  28.  
  29.     every write(lftx(1 to 5, "."))
  30.  
  31.     f := partial(left, "y", 5)
  32.     every write(f(!"abcd"))
  33.  
  34.     every w := partial(write, "testing..." | "MORE testing...") do
  35.         every w(1 to 5)
  36.     
  37.     s := set()
  38.     p := partial(insert,s)
  39.     p(1)
  40.     p(2)
  41.     p(s)
  42.     write(Image(s))
  43. end
  44.     
  45. Here's the output:
  46.  
  47. x
  48. x.
  49. x..
  50. x...
  51. x....
  52. yaaaa
  53. ybbbb
  54. ycccc
  55. ydddd
  56. testing...1
  57. testing...2
  58. testing...3
  59. testing...4
  60. testing...5
  61. MORE testing...1
  62. MORE testing...2
  63. MORE testing...3
  64. MORE testing...4
  65. MORE testing...5
  66. S1:[
  67.   2,
  68.   1,
  69.   S1]
  70.  
  71. Here's the hack...
  72.     
  73. record partial_record(proc, args)
  74. global partials
  75. procedure partial(args[])
  76.     static partial_num
  77.     initial {
  78.         partials := table()
  79.         partial_num := 0
  80.         }
  81.     partial_num +:= 1
  82.     partials[partial_num] := partial_record(args[1], args[2:0])
  83.     return "do_partial_" || partial_num
  84. end
  85. #
  86. # I need a way to make these on the fly! Until then...
  87. #
  88. procedure do_partial_1(args[])
  89.     r := partials[1]
  90.     suspend r.proc!(r.args|||args)
  91. end
  92. procedure do_partial_2(args[])
  93.     r:= partials[2]
  94.     suspend r.proc!(r.args|||args)
  95. end
  96. procedure do_partial_3(args[])
  97.     r:= partials[3]
  98.     suspend r.proc!(r.args|||args)
  99. end
  100. procedure do_partial_4(args[])
  101.     r:= partials[4]
  102.     suspend r.proc!(r.args|||args)
  103. end
  104. procedure do_partial_5(args[])
  105.     r:= partials[5]
  106.     suspend r.proc!(r.args|||args)
  107. end
  108.  
  109.  
  110. Pop quiz for comparative languages students: The above implementation
  111. relies on a procedure named "partial" to indicate that partial evaluation
  112. should be done.  In ML such a cue is not necessary.  Can partial evaluation
  113. be added to Icon without requiring a cue of some sort?
  114.